using Microsoft.Phone.Info;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
using System.Windows.Threading;
namespace MicroMsg.UI.Util
{
	public static class MemoryDiagnosticsHelper
	{
		private static Popup popup;
		private static TextBlock currentMemoryBlock;
		private static DispatcherTimer timer;
		private static bool forceGc;
		private static string sStrAppUseMem = "";
		private static string sStrAppCanUseMem = "";
		private static string sStrDeviceMem = "";
		private static long sNtimerCnt = 0L;
		public static void Start(bool forceGc)
		{
			MemoryDiagnosticsHelper.forceGc = forceGc;
			MemoryDiagnosticsHelper.CreatePopup();
			MemoryDiagnosticsHelper.CreateTimer();
			MemoryDiagnosticsHelper.ShowPopup();
			MemoryDiagnosticsHelper.StartTimer();
		}
		private static void ShowPopup()
		{
			MemoryDiagnosticsHelper.popup.set_IsOpen(true);
		}
		private static void StartTimer()
		{
			MemoryDiagnosticsHelper.timer.Start();
		}
		private static void CreateTimer()
		{
			if (MemoryDiagnosticsHelper.timer != null)
			{
				return;
			}
			MemoryDiagnosticsHelper.timer = new DispatcherTimer();
			MemoryDiagnosticsHelper.timer.set_Interval(TimeSpan.FromMilliseconds(300.0));
			MemoryDiagnosticsHelper.timer.add_Tick(new EventHandler(MemoryDiagnosticsHelper.timer_Tick));
		}
		private static void timer_Tick(object sender, EventArgs e)
		{
			if (MemoryDiagnosticsHelper.forceGc)
			{
				GC.Collect();
			}
			long applicationCurrentMemoryUsage = DeviceStatus.get_ApplicationCurrentMemoryUsage();
			if (0L == MemoryDiagnosticsHelper.sNtimerCnt)
			{
				long applicationMemoryUsageLimit = DeviceStatus.get_ApplicationMemoryUsageLimit();
				MemoryDiagnosticsHelper.sStrAppCanUseMem = ",可用内存:" + string.Format("{0:N}", applicationMemoryUsageLimit / 1024L);
				long deviceTotalMemory = DeviceStatus.get_DeviceTotalMemory();
				MemoryDiagnosticsHelper.sStrDeviceMem = ",设备总内存:" + string.Format("{0:N}", deviceTotalMemory / 1024L);
			}
			MemoryDiagnosticsHelper.sStrAppUseMem = "程序使用内存: " + string.Format("{0:N}", applicationCurrentMemoryUsage / 1024L);
			MemoryDiagnosticsHelper.currentMemoryBlock.set_Text(MemoryDiagnosticsHelper.sStrAppUseMem + MemoryDiagnosticsHelper.sStrAppCanUseMem + MemoryDiagnosticsHelper.sStrDeviceMem);
			MemoryDiagnosticsHelper.sNtimerCnt += 1L;
		}
		private static void CreatePopup()
		{
			if (MemoryDiagnosticsHelper.popup != null)
			{
				return;
			}
			MemoryDiagnosticsHelper.popup = new Popup();
			double fontSize = (double)Application.get_Current().get_Resources().get_Item("PhoneFontSizeSmall") - 2.0;
			Brush foreground = (Brush)Application.get_Current().get_Resources().get_Item("PhoneForegroundBrush");
			StackPanel stackPanel = new StackPanel();
			stackPanel.set_Orientation(1);
			stackPanel.set_Background((Brush)Application.get_Current().get_Resources().get_Item("PhoneSemitransparentBrush"));
			StackPanel stackPanel2 = stackPanel;
			TextBlock textBlock = new TextBlock();
			textBlock.set_Text("---");
			textBlock.set_FontSize(fontSize);
			textBlock.set_Foreground(foreground);
			MemoryDiagnosticsHelper.currentMemoryBlock = textBlock;
			stackPanel2.get_Children().Add(MemoryDiagnosticsHelper.currentMemoryBlock);
			UIElement arg_112_0 = stackPanel2;
			CompositeTransform compositeTransform = new CompositeTransform();
			compositeTransform.set_Rotation(90.0);
			compositeTransform.set_TranslateX(480.0);
			compositeTransform.set_TranslateY(100.0);
			compositeTransform.set_CenterX(0.0);
			compositeTransform.set_CenterY(0.0);
			arg_112_0.set_RenderTransform(compositeTransform);
			MemoryDiagnosticsHelper.popup.set_Child(stackPanel2);
		}
		public static void Stop()
		{
			MemoryDiagnosticsHelper.HidePopup();
			MemoryDiagnosticsHelper.StopTimer();
		}
		private static void StopTimer()
		{
			MemoryDiagnosticsHelper.timer.Stop();
		}
		private static void HidePopup()
		{
			MemoryDiagnosticsHelper.popup.set_IsOpen(false);
		}
	}
}
